package cubelabs.nexus.dao;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import cubelabs.nexus.entity.CounsellingEvent;
import cubelabs.nexus.entity.CounsellingEventStatus;
import cubelabs.nexus.entity.Organization;
import cubelabs.nexus.entity.User;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;

@Repository("organizationDAO")
public class OrganizationDAO extends BaseDAO<Organization, Long> {

    public List<Organization> findByUser(Long userId) {
        List<Organization> organizationsList = findByCriteria(Restrictions.and(Restrictions.eq("user.id", userId)));
        return organizationsList;
    }

    public List<Organization> getWorkingOrganizations(Long userId) {
        String queryStr = "SELECT DISTINCT org.* FROM Organization org, OrganizationRequest orgr "
                + "WHERE org.id=orgr.organization_id and (org.user_id ="+userId+" or ( orgr.user_id="+userId+" and orgr.accepted = true))";
        SQLQuery query =  (SQLQuery) getSession().createSQLQuery(queryStr);
            query.addEntity(getPersistentClass());
         return query.list();
    }
    
    public List<Organization> getMyOrganizations(User user) {
    	Criteria crit = getSession().createCriteria(getPersistentClass());
		crit.add(Restrictions.eq("user", user));
		List<Organization> organizationList = crit.list();
		return organizationList;
    	
    }
}
